Jazzila.prototype.onStateChange = function (state){
}

function getDiv(text){
	var div = document.createElement ("span");
	div.appendChild(document.createTextNode(text));
	return div;
}

function getTime(dt){
	var stamp = new Date(dt);
	var min = "0" + stamp.getMinutes() + "]";
	var hr = "0" + stamp.getHours() + ":";
 	return "[" + hr.substr(hr.length - 3) + min.substr(min.length - 3);
}

function getTableRow(jid, msg, dt){
	var cell = document.createElement("html:td");
	cell.appendChild(getDiv(getTime(dt))).setAttribute("class", "time");
	cell.appendChild(jid);
	cell.appendChild(msg);
	var row = document.createElement("html:tr")
	row.appendChild(cell);
	return(row);
}

TreeListener.prototype.onUnassert= function (ds, source, property, target)
{
	var rdf = CC["@mozilla.org/rdf/rdf-service;1"].getService(CI.nsIRDFService);
	var thread = source.Value.substr(0, source.Value.lastIndexOf("/"));
	var msg = document.getElementById(source.Value);
	if (msg)
		document.removeChild(msg);
}

TreeListener.prototype.onAssert = function (ds, source, property, target)
{
	var rdf = CC["@mozilla.org/rdf/rdf-service;1"].getService(CI.nsIRDFService);
	var thread = source.Value.substr(0, source.Value.lastIndexOf("/"));
	if(thread!= "urn:xmpp:message:" + window.name)
		return;
	if (property.Value == "http://xmpp.org/RDF#msg")
	{
		var jid = getDiv("<" + getRDFLiteral(ds, source, rdf.GetResource ("http://xmpp.org/RDF#jid"))+">");
		var timestamp = getRDFLiteral(ds, source, rdf.GetResource ("http://xmpp.org/RDF#timestamp"));
		var style = getRDFLiteral(ds, source, rdf.GetResource ("http://xmpp.org/RDF#style"));
		jid.setAttribute("class", style);
		var msg = getDiv(target.QueryInterface(CI.nsIRDFLiteral).Value);
		var tbl = document.getElementById("output-body");
		if (tbl){
			var row = getTableRow(jid, msg, timestamp);
			row.setAttribute("id", source.Value);
			tbl.appendChild(row);
			window.scrollTo(0, document.height);
		}
	}
}

var jTL = null;

function onLoadOrig()
{
	var tbl = document.getElementById("output-body");
	if (tbl)
	{
		var rdf = CC["@mozilla.org/rdf/rdf-service;1"].getService(CI.nsIRDFService);
		var rdfCU = CC["@mozilla.org/rdf/container-utils;1"].getService(CI.nsIRDFContainerUtils); 
		var urn = rdf.GetResource("urn:xmpp:chat:"+window.name);
		var ctn = rdfCU.MakeSeq(ds, urn);
		var etor = ctn.GetElements();
		var rsTimestamp = rdf.GetResource("http://xmpp.org/RDF#timestamp");
		var rsJid = rdf.GetResource("http://xmpp.org/RDF#jid");
		var rsMsg = rdf.GetResource("http://xmpp.org/RDF#msg");
		var rsStyle = rdf.GetResource("http://xmpp.org/RDF#style");
		while (etor.hasMoreElements())
		{
			var elem = etor.getNext().QueryInterface(CI.nsIRDFResource);
			var jid = getDiv("<"+getRDFLiteral(ds, elem, rsJid)+">");
			jid.setAttribute("class", getRDFLiteral(ds, elem, rsStyle));
			var msg = getDiv(getRDFLiteral(ds, elem, rsMsg));
			tbl.appendChild(getTableRow(jid, msg, getRDFLiteral(ds, elem, rsTimestamp)));
		}
	}
	window.scrollTo(0, document.height);
	window.removeEventListener("load", onLoad, true);
}


function fillChat(tbl)
{
    var doc = document;
    var rdf = CC["@mozilla.org/rdf/rdf-service;1"].getService(CI.nsIRDFService);
    var rdfCU = CC["@mozilla.org/rdf/container-utils;1"].getService(CI.nsIRDFContainerUtils); 
    var urn = rdf.GetResource("urn:xmpp:chat:"+window.name);
    var ctn = rdfCU.MakeSeq(ds, urn);
    var etor = ctn.GetElements();
    var rsTimestamp = rdf.GetResource("http://xmpp.org/RDF#timestamp");
    var rsJid = rdf.GetResource("http://xmpp.org/RDF#jid");
    var rsMsg = rdf.GetResource("http://xmpp.org/RDF#msg");
    var rsStyle = rdf.GetResource("http://xmpp.org/RDF#style");
    while (etor.hasMoreElements())
    {
	    var elem = etor.getNext().QueryInterface(CI.nsIRDFResource);
	    var message = doc.createElement("message");
	    message.setAttribute("jid",  getRDFLiteral(ds, elem, rsJid)+":");
	    message.setAttribute("time", getTime(getRDFLiteral(ds, elem, rsTimestamp)));
	    message.setAttribute("body", getRDFLiteral(ds, elem, rsMsg));
	    message.setAttribute("class", getRDFLiteral(ds, elem, rsStyle));
	    tbl.appendChild(message);
	    var lbl = doc.createElement("label");
    }

}

function onLoad()
{
    ds  = jazzila.getDataSource();
    var box = document.getElementById("output-box");
    if (box)
	    fillChat(box);
    jTL = new TreeListener();
    ds.AddObserver(jTL);
    window.scrollTo(0, document.height);
    window.removeEventListener("load", onLoad, true);
}


Jazzila.prototype.onClick = function (event)
{
	switch (event.button)
	{
	case 2:
		this.showContextMenu(event.clientX, event.clientY, "chat-context-menu", "output-body");		
		break;
	};
}

function onUnload()
{
//	var rdf = CC["@mozilla.org/rdf/rdf-service;1"].getService(CI.nsIRDFService);
//	var ds  = rdf.GetDataSource("file:///C|/messages.rdf");
	if (ds)
		ds.RemoveObserver(jTL);
	jTL = null;
}
var ds  = null;
window.addEventListener("load", onLoad, true);
window.addEventListener("unload", onUnload, true);
